package de.gameduell.framework.asset {
	import de.gameduell.framework.asset.AssetEvent;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;	

	/**
	 * @author GD
	 */
	public class Asset extends EventDispatcher implements IAsset{

		protected var _id:String;
		protected var _url:String;
		protected var _type:String;
		protected var _httpStatus:int;
		protected var _error:String;
		protected var _useSameDomain:Boolean;

		public function Asset(aId:String, aUrl:String, useSameDomain:Boolean = true) {
			_useSameDomain = useSameDomain;
			_id    = aId;
			_url   = aUrl;
			_error = "";
		}

		public function get percentLoaded():int{
			return 0;
		}
	
		public function get id():String {
			return _id;
		}

		public function get url():String {
			return _url;
		}

		public function get type():String {
			return _type;
		}
		
		public function get data():Object {
			return null;
		}
		
		public function get httpStatus():int {
			return _httpStatus;
		}
		
		public function get error():String{
			return _error;
		}
		
		public function load():void{
			
		}
		
		public function registerLoader(aLoader:EventDispatcher):void{
			aLoader.addEventListener(Event.COMPLETE, complete);
			aLoader.addEventListener(ProgressEvent.PROGRESS, progress);
			aLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			aLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpResponseHandler);
		}
		
		protected function httpResponseHandler(event:HTTPStatusEvent):void {
			_httpStatus = event.status;
		}

		protected function progress(event:Event = null):void {
			dispatchEvent(new AssetEvent(AssetEvent.PROGRESS, this));
		}

		protected function complete(event:Event = null):void {
			dispatchEvent(new AssetEvent(AssetEvent.COMPLETE,this));
		}
		
		protected function ioErrorHandler(event:IOErrorEvent):void {
			var errorEvent:AssetEvent = new AssetEvent(AssetEvent.LOAD_ERROR,this);
			_error = event.text;
			dispatchEvent(errorEvent);
		}
		
//		public function get loader():Loader{
//			return null;
//		}
			
	}
}
